<?php
/**
 * 我的商城
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */



defined('ByShopWWI') or exit('Access Invalid!');

class member_indexControl extends mobileMemberControl {

    public function __construct(){
        parent::__construct();
    }

    /**
     * 我的商城
     */
    public function indexOp() {
        $member_info = array();
		$member_info = $this->getMemberAndGradeInfo(true);
        $member_info['user_name'] = $this->member_info['member_name'];
		$member_info['id'] = $this->member_info['member_id'];
		
		if(strstr($member_info['member_avatar'],'.png')){
		$member_info['avator']= UPLOAD_SITE_URL.'/'.ATTACH_AVATAR.'/'.$member_info['member_avatar'];

		}elseif(strstr($member_info['member_avatar'],'.jpg')){
			$member_info['avator']= UPLOAD_SITE_URL.'/'.ATTACH_AVATAR.'/'.$member_info['member_avatar'];
	
		}else{

			$member_info['avator']= UPLOAD_SITE_URL.'/'.ATTACH_COMMON.DS.C('default_user_portrait');

		}
       // $member_info['avator'] = getMemberAvatarForID($this->member_info['member_id']);

        $member_info['point'] = $this->member_info['member_points'];
        $member_info['predepoit'] = $this->member_info['available_predeposit'];
        $member_info['available_rc_balance'] = $this->member_info['available_rc_balance'];
		$member_info['level_name'] = $member_info['level_name'];	
		unset($member_info['member_privacy']);
		unset($member_info['weixin_info']);
		unset($member_info['member_qqinfo']);
		/*用户地址
		$model_area = Model('area');
        $condition = array(); 
        $condition['area_id'] = $member_info['member_areaid'];
        $area_info = $model_area->getAreaList($condition, 'area_id,area_name');
		$member_info['area']=$area_info[0]['area_name'];
		$condition['area_id'] = $member_info['member_cityid'];
        $area_info = $model_area->getAreaList($condition, 'area_id,area_name');
		$member_info['city']=$area_info[0]['area_name'];
		$condition['area_id'] = $member_info['member_provinceid'];
        $area_info = $model_area->getAreaList($condition, 'area_id,area_name');
		$member_info['province']=$area_info[0]['area_name'];
		*/		
		$favorites_model = Model('favorites');
		$member_info['favorites_store'] = $favorites_model->getStoreFavoritesCountByStoreId('',$this->member_info['member_id']);//店铺收藏数
		$member_info['favorites_goods'] = $favorites_model->getGoodsFavoritesCountByGoodsId('',$this->member_info['member_id']);//商品收藏数
		//生成二维码　
		include "../phpqrcode/phpqrcode.php";
		
		$value = "http://app.puhuiwww.com/tmpl/member/register.html?rec=" .$this->member_info['member_id'];
		$errorCorrectionLevel = "L";
		$matrixPointSize = "4";
		$path = '../phpqrcode/codes/';
		$fileName = $path . 'code_' .$this->member_info['member_id']. '.png';
		
		QRcode::png($value, $fileName, $errorCorrectionLevel, $matrixPointSize);
		$member_info['code_img'] = "http://app.puhuiwww.com/phpqrcode/codes/code_".$this->member_info['member_id'].".png";
		
        output_data(array('member_info' => $member_info));
    }
	
	/**
     * 我的积分
     */
    public function my_assetOp() {
		$member_info = $this->getMemberAndGradeInfo(true);
		$point = $this->member_info['member_points'];
		$predepoit = $this->member_info['available_predeposit'];
		$balance = $this->member_info['available_rc_balance'];
		$kucun_point = $this->member_info['kucun_points'];
		$voucher =  Model('voucher')->getCurrentAvailableVoucherCount($this->member_info['member_id']); //取得当前有效代金券数量 
		$redpacket =  Model('redpacket')->getCurrentAvailableRedpacketCount($this->member_info['member_id']); //取得当前有效红包数量
		$fields =$_GET["fields"];
		if(!$fields){
			$fields=$_POST['fields'];
		}
		if($fields=='predepoit'){//可用预存款
			output_data(array('predepoit' => $predepoit));
		}elseif($fields=='available_rc_balance'){//可用充值卡
			output_data(array('available_rc_balance' => $balance));
		}else{
			output_data(array('point' => $point,'predepoit'=>$predepoit,'available_rc_balance'=>$balance,'redpacket'=>$redpacket,'voucher'=>$voucher,'kucun_point'=>$kucun_point));
		}
	}
	protected function getMemberAndGradeInfo($is_return = false){
        $member_info = array();
        //会员详情及会员级别处理
        if($this->member_info['member_id']) {
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($this->member_info['member_id']);
            if ($member_info){
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_info['member_exppoints']));
                $member_info = array_merge($member_info,$member_gradeinfo);
                $member_info['security_level'] = $model_member->getMemberSecurityLevel($member_info);
            }
        }
        if ($is_return == true){//返回会员信息
            return $member_info;
        } else {//输出会员信息
            Tpl::output('member_info',$member_info);
        }
    }

	//修改资料
		function update_memberOp(){
		$contion['member_id'] = $this->member_info['member_id'];

		if(!empty($_POST['name'])){
			$data['member_name'] = $_POST['name'];
		}
		if(!empty($_POST['area_id'])){
			$data['member_areaid'] = $_POST['area_id'];
		}
		if(!empty($_POST['city_id'])){
			$data['member_cityid'] = $_POST['city_id'];
		}
		if(!empty($_POST['province_id'])){
			$data['member_provinceid'] = $_POST['province_id'];
		}
		if(!empty($_POST['mobile'])){
			$data['member_mobile'] = $_POST['mobile'];
		}
		if(!empty($data)){
			$model_member = Model('member');
			$result =$model_member->editMember($contion,$data);
			if($result){
				output_data('1');
			}else{
				output_error('修改失败');
			}
		}else{
			output_data('1');
		}
	}
		//修改头像
	function updimg_memberOp(){
		header('Content-type: text/html; charset=UTF-8');
		$contion['member_id'] = $this->member_info['member_id'];
		
		$base64 = $_POST["head"]; // 得到参数
		$img = base64_decode($base64); // 将格式为base64的字符串解码
		$path = "../data/upload/shop/avatar/".time()."_".$this->member_info['member_id'].".png"; // 产生随机唯一的名字作为文件名
		file_put_contents($path, $img); // 将图片保存到相应位置*/
		$data['member_avatar'] = time().'_'.$this->member_info['member_id'].'.png';
		if(!empty($data)){
			$model_member = Model('member');
			$result =$model_member->editMember($contion,$data);
			if($result){
				output_data('成功');
			}else{
				output_error('修改失败');
			}
		}else{
			output_data('2');
		}
	}
	
	//资料完善
	function member_informationOp(){
		$contion['member_id'] = $this->member_info['member_id'];
		
		if(!empty($_POST['truename'])){
			$data['member_truename'] = $_POST['truename'];
		}
		if(!empty($_POST['identification_card'])){
			$data['identification_card'] = $_POST['identification_card'];
		}
		if(!empty($_POST['mobile'])){
			$data['member_mobile'] = $_POST['mobile'];
		}
		if(!empty($data)){
			$model_member = Model('member');

			$result =$model_member->editMember($contion,$data);
			if($result){
			     output_data('成功');
			}
                        else{
			     output_error('修改失败');
			}
		}else{
			output_data('1');
		}
		
	}
	//会员数据统计
	public function count_memberOp(){
		$member_id =$this->member_info['member_id'];
		$model_member = Model('member');
		//新增积分 分红豆turncash  redpoints 宝豆turnpoints
		$point = $this->member_info['member_points'];//宝豆
		$predepoit = $this->member_info['available_predeposit'];
		//分红豆turncash
		$red_points = $model_member->getMemberInfo(array('member_id'=>$member_id),'member_red_points',true);
		output_data(array('point' => $point,'predepoit'=>$predepoit,'red_points'=>$red_points['member_red_points']));
		
	}
	 /**
     * 忘记密码
     */
    public function update_passwordOp(){
        $model_member   = Model('member');
		$contion['member_id'] = $this->member_info['member_id'];
        $register_info = array();
         $contion['member_mobile'] = $_POST['mobile'];
        $register_info['member_passwd'] = md5($_POST['password']);
		$mber_info = $model_member->getMemberInfo($contion);

		if(empty($mber_info)){
			output_error('请先绑定手机');
		}
       
        $member_info = $model_member->editMember($contion,$register_info);
        if($member_info) {
            output_data('1');
        } else {
            output_error('修改失败');
        }

    }
	
	/**
     * AJAX验证
     *
     */
	protected function check(){
        if (checkSeccode($_POST['nchash'],$_POST['captcha'])){
            return true;
        }else{
            return false;
        }
    }

}
